package model

import (
	"crypto/md5"
	"encoding/hex"
	"gitee.com/jinmingzhi/kit/rdb"
	"time"
)

type User struct {
	ID                uint32   `db:"id"`
	User              string   `db:"user"`
	Passwd            string   `db:"passwd"`
	Gender            bool     `db:"gender"`              // 性别  true 男
	RegistratonNumber string   `db:"registration_number"` // 准考证号
	StudentID         string   `db:"student_id"`          // 考生学号
	Name              string   `db:"name"`                // 姓名
	ImageUrl          string   `db:"image_url"`           // 考生照片
	CreateTime        rdb.Time `db:"create_time"`
	DeleteTime        rdb.Time `db:"delete_time"`
}

// 学籍状态
type StudentStatus int32

const (
	STUDENTSTATUS_STUDYING      StudentStatus = iota // 学习中
	STUDENTSTATUS_UNDERGRADUATE                      // 肄业
	STUDENTSTATUS_GRADUATE                           // 毕业
)

// 职业状态
type OccupationStatus int32

const (
	OCCUPATIONSTATS_UNWORKING OccupationStatus = iota // 未工作
	OCCUPATIONSTATS_WORKING                           // 工作
)

// 证书类别
type CertificateType int32

const (
	CERTIFICATETYPE_NONE      CertificateType = iota // 无学历
	CERTIFICATETYPE_SPECIALTY                        // 专业
)

// 培训层次
type CultivationLevel int32

const (
	CULTIVATIONLEVEL_JUNIOR_COLLEGE CultivationLevel = iota
	CULTIVATIONLEVEL_COLLEGE_UPGRADE
)

type UserDetail struct {
	ID               uint32           `db:"id"`
	Uid              uint32           `db:"uid"`
	Nation           string           `db:"nation"`            // 名族
	StudentStatus    StudentStatus    `db:"student_status"`    // 学籍状态
	OccupationStatus OccupationStatus `db:"occupation_status"` // 职业状态
	CertificateType  CertificateType  `db:"certificate_type"`  // 证书类别
	SchoolName       string           `db:"school_name"`       // 高校名称
	CultivationLevel CultivationLevel `db:"cultivation_level"` // 培训层次
	IDNumber         string           `db:"id_number"`         // 身份证号
}

func NewUser(user, passwd string, gender bool, registration_number, student_id, name string) *User {
	return &User{
		User:              user,
		Passwd:            EncrypPasswd(passwd),
		Gender:            gender,
		RegistratonNumber: registration_number,
		StudentID:         student_id,
		Name:              name,
		ImageUrl:          "",
		CreateTime:        rdb.FromTime(time.Now()),
	}
}

func NewUserDetail(uid uint32, nation, id_number, school_name string, student_status StudentStatus, occupation_status OccupationStatus, certificate_type CertificateType, cultivation_level CultivationLevel) *UserDetail {
	return &UserDetail{
		Uid:              uid,
		Nation:           nation,
		StudentStatus:    student_status,
		OccupationStatus: occupation_status,
		CertificateType:  certificate_type,
		SchoolName:       school_name,
		CultivationLevel: cultivation_level,
		IDNumber:         id_number,
	}
}

func EncrypPasswd(passwd string) string {
	h := md5.New()
	h.Write([]byte(passwd))
	return hex.EncodeToString(h.Sum(nil))
}


func (u *User) ComParePasswd(passwd string) bool {
	if EncrypPasswd(passwd) == u.Passwd {
		return true
	}
	return false
}
